package action;

import com.alibaba.fastjson.JSONObject;
import util.AESUtils;
import util.Const;
import util.HttpUtil;

import java.net.SocketTimeoutException;
import java.util.HashMap;

/**
 * Created by zhangting on 03/04/2019.
 */
public class ActionHelper {

    private static ActionHelper actionHelper = new ActionHelper();
    private JSONObject requestUrl =new JSONObject();
    private ActionHelper(){}

    public static ActionHelper getInstance(){
        return actionHelper;
    }

    // url 请求对方的url 地址
    public void register(String url){
        if(null==url ||"".equals(url)){
            url= Const.REGISTER_URL;
        }
        String param="";
        JSONObject json = new JSONObject();
        json.put("url",Const.SELF_URL);
        json.put("key",DoAction.key);
        try {
            String body = HttpUtil.stream2String(HttpUtil.sendPost(url,json.toJSONString()));
            System.out.println("===register() :" + body);

            body = AESUtils.aesDecrypt(body,DoAction.key);
            System.out.println("decrypt ===register() :" + body);

            requestUrl= JSONObject.parseObject(body);

        } catch ( Exception e) {
            e.printStackTrace();
        }
    }

    public String sendFriendMsg(String fromqq,String toqq,String msg){
            JSONObject json = new JSONObject();
            json.put("fromqq",fromqq);
            json.put("friendqq",toqq);
            json.put("msg",msg);
            String result = HttpUtil.stream2String(HttpUtil.sendPost(requestUrl.getString("sendFriendMsg"),AESUtils.aesEncrypt(json.toJSONString(),DoAction.key)));

            return AESUtils.aesDecrypt(result,DoAction.key);

    }
    public String sendGroupMsg(String fromqq ,String groupqq,String msg){
            JSONObject json = new JSONObject();
            json.put("fromqq",fromqq);
            json.put("groupqq",groupqq);
            json.put("msg",msg);
            String result = HttpUtil.stream2String(HttpUtil.sendPost(requestUrl.getString("sendGroupMsg"),AESUtils.aesEncrypt(json.toJSONString(),DoAction.key)));

            return AESUtils.aesDecrypt(result,DoAction.key);

    }
    public String sendTempMsg(String fromqq ,String toqq,String groupqq,String msg){
            JSONObject json = new JSONObject();
            json.put("fromqq",fromqq);
            json.put("groupqq",groupqq);
            json.put("toqq",toqq);
            json.put("msg",msg);
            String result = HttpUtil.stream2String(HttpUtil.sendPost(requestUrl.getString("sendTemporaryMsg"),AESUtils.aesEncrypt(json.toJSONString(),DoAction.key)));

            return AESUtils.aesDecrypt(result,DoAction.key);

    }

    public String getFriendList(String qq){
        JSONObject json = new JSONObject();
        json.put("clientqq",qq);
        String result = HttpUtil.stream2String(HttpUtil.sendPost(requestUrl.getString("getFriendList"),AESUtils.aesEncrypt(json.toJSONString(),DoAction.key)));

        return AESUtils.aesDecrypt(result,DoAction.key);

    }
    public String getGroupList(String qq){
        JSONObject json = new JSONObject();
        json.put("clientqq",qq);
        String result = HttpUtil.stream2String(HttpUtil.sendPost(requestUrl.getString("getGroupList"),AESUtils.aesEncrypt(json.toJSONString(),DoAction.key)));

        return AESUtils.aesDecrypt(result,DoAction.key);

    }


}
